package cn.iocoder.yudao.module.system.service.userordermanage;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.userordermanage.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userordermanage.UserOrderManageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.userordermanage.UserOrderManageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 订单管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserOrderManageServiceImpl implements UserOrderManageService {

    @Resource
    private UserOrderManageMapper userOrderManageMapper;

    @Override
    public Long createUserOrderManage(UserOrderManageSaveReqVO createReqVO) {
        // 插入
        UserOrderManageDO userOrderManage = BeanUtils.toBean(createReqVO, UserOrderManageDO.class);
        userOrderManageMapper.insert(userOrderManage);
        // 返回
        return userOrderManage.getId();
    }

    @Override
    public void updateUserOrderManage(UserOrderManageSaveReqVO updateReqVO) {
        // 校验存在
        validateUserOrderManageExists(updateReqVO.getId());
        // 更新
        UserOrderManageDO updateObj = BeanUtils.toBean(updateReqVO, UserOrderManageDO.class);
        userOrderManageMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserOrderManage(Long id) {
        // 校验存在
        validateUserOrderManageExists(id);
        // 删除
        userOrderManageMapper.deleteById(id);
    }

    private void validateUserOrderManageExists(Long id) {
        if (userOrderManageMapper.selectById(id) == null) {
            throw exception(USER_ORDER_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public UserOrderManageDO getUserOrderManage(Long id) {
        return userOrderManageMapper.selectById(id);
    }

    @Override
    public PageResult<UserOrderManageDO> getUserOrderManagePage(UserOrderManagePageReqVO pageReqVO) {
        return userOrderManageMapper.selectPage(pageReqVO);
    }

}